<template>
  <div class="container">
    <el-card>
      <p>吴进先</p>

      <!-- 面包屑区域 -->
      <el-breadcrumb
        separator-class="el-icon-arrow-right"
        v-if="subjects.id && subjects.name"
      >
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{
          subjects.id ? subjects.name : ''
        }}</el-breadcrumb-item>
        <el-breadcrumb-item>标签管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 搜索nav栏 -->
      <el-row>
        <el-col :span="18">
          <!-- 搜索框 -->
          <el-col :span="11">
            <el-form ref="subjectFrom" :model="queryList" label-width="80px">
              <el-form-item label="标签名称">
                <el-input
                  class="inputCol"
                  v-model="queryList.tagName"
                ></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="queryList.state" placeholder="请选择">
                  <el-option label="已启用" :value="1"> </el-option>
                  <el-option label="已禁用" :value="0"> </el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="12" class="left-fot">
            <el-button size="small" class="colInput" plain @click="resetInput"
              >清除</el-button
            >
            <el-button
              size="small"
              class="colInput"
              type="primary"
              @click="getDirectList"
              >搜索</el-button
            >
          </el-col>
        </el-col>
        <el-col :span="6">
          <el-button
            class="colright"
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="addDialogVisible = true"
            >新增标签</el-button
          >
          <el-button
            v-if="subjects.id && subjects.name"
            type="text"
            class="colright"
            icon="el-icon-back"
            @click="$router.back()"
            >返回学科</el-button
          >
        </el-col>
      </el-row>
      <el-alert
        :title="'数据共' + total + '条'"
        type="info"
        :closable="false"
        show-icon
      >
      </el-alert>
      <!-- tab表格 -->
      <el-table ref="singleTable" :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column property="subjectName" label="所属学科" width="120">
        </el-table-column>
        <el-table-column property="tagName" label="标签名称" width="120">
        </el-table-column>
        <el-table-column property="username" label="创建者"> </el-table-column>
        <el-table-column property="addDate" label="创建日期"> </el-table-column>
        <el-table-column property="totals" label="面试题数量">
        </el-table-column>
        <el-table-column property="state" label="状态">
          <template v-slot="{ row }">
            {{ row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column property="address" label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="editState(row)">{{
              row.state === 1 ? '启用' : '禁用'
            }}</el-button>
            <el-button
              type="text"
              :disabled="row.state === 0"
              @click="editDirectorys(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 0"
              @click="removeDir(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="tablePage.pagesize"
        :page-sizes="number"
        layout="prev, pager, next,sizes,jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 对话话框 -->
    <el-dialog
      :title="(directorysForm.id ? '修改' : '新增') + '标签'"
      :visible.sync="addDialogVisible"
      width="20%"
      @close="onClose"
    >
      <el-form
        ref="form"
        :model="directorysForm"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item
          v-if="!subjects.id && !subjects.name"
          label="所属学科"
          prop="value"
        >
          <el-select v-model="directorysForm.value" placeholder="请选择">
            <el-option
              v-for="item in tableData"
              :key="item.id"
              :label="item.subjectName"
              :value="item.subjectID"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input
            v-model="directorysForm.tagName"
            placeholder="请输入标签名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="determine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { list, update, add, changeState, remove } from '@/api/hmmm/tags.js'
export default {
  name: 'tags',
  data () {
    return {
      queryList: {
        tagName: '',
        state: null
      },
      tableData: [],
      tablePage: {
        page: 1,
        pagesize: 10
      },
      subjectID: '',
      directorysForm: {
        tagName: '',
        value: ''
      },
      number: [5, 10, 20, 50],
      rules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        value: [{ required: true, message: '请选择所属学科', trigger: 'blur' }]
      },
      total: 0,
      addDialogVisible: false
    }
  },
  computed: {
    subjects () {
      return this.$route.query || null
    }
  },
  created () {
    this.loadTags()
  },
  methods: {
    // 目录列表
    async loadTags () {
      this.queryList.subjectID = this.subjects.id || null
      const { data: res } = await list({ ...this.tablePage, ...this.queryList })
      res.items.forEach(item => {
        item.addDate = this.$dayjs(item.addDate).format('YYYY-MM-DD hh:mm:ss')
      })
      this.total = res.counts
      this.tableData = res.items
    },
    // 修改
    editDirectorys (row) {
      this.directorysForm.value = row.subjectID
      this.directorysForm.tagName = row.tagName
      this.directorysForm.id = row.id
      this.addDialogVisible = true
    },
    // 清除
    resetInput () {
      if (this.queryList.tagName === '') {
        this.queryList.tagName = ''
        this.queryList.state = null
      } else {
        this.queryList.tagName = ''
        this.loadTags()
      }
    },
    // 搜索
    async getDirectList () {
      if (this.queryList.tagName === '') {
        return ''
      } else {
        this.loadTags()
      }
    },
    async editState (row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        this.$message.success('操作成功')
        row.state = row.state === 1 ? 0 : 1
      } catch (err) {
        this.$message.error('请求错误!')
      }
    },
    async determine () {
      await this.$refs.form.validate()
      if (this.directorysForm.id) {
        await update({
          id: this.directorysForm.id,
          tagName: this.directorysForm.tagName,
          subjectID: (this.subjectID = this.directorysForm.value)
        })
        this.$message.success('编辑成功')
      } else {
        await add({
          tagName: this.directorysForm.tagName,
          subjectID: (this.subjectID = this.directorysForm.value)
        })
        this.$message.success('新增成功')
      }
      this.loadTags()
      this.addDialogVisible = false
    },
    // 关闭对话框
    onClose () {
      this.$refs.form.resetFields()
      this.addDialogVisible = false
      this.directorysForm = {
        tagName: '',
        value: ''
      }
    },
    // 删除
    async removeDir (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row.id)
          this.$message.success('删除成功!')
          this.loadTags()
        })
        .catch(() => {})
    },
    // 分页
    handleSizeChange (val) {
      this.tablePage.pagesize = val
      this.loadTags()
    },
    handleCurrentChange (val) {
      this.tablePage.page = val
      this.loadTags()
    }
  }
}
</script>

<style scoped lang='less'>
.left-fot {
  margin-left: 9px;
}
.el-row {
  margin-top: 20px;
}
.colInput {
  height: 35px;
}
.colright {
  float: right;
  height: 35px;
  margin-left: 10px;
}
.el-breadcrumb {
  padding-bottom: 20px;
}
.inputCol {
  width: 200px;
}
.el-form-item {
  display: inline-block;
}
.el-table {
  margin-top: 10px;
}
.el-pagination {
  float: right;
  margin: 20px 0;
}
.elform {
  width: 100%;
}

.el-form-item__content {
  margin-left: 10px;
}
</style>
